package com.demo;

import com.demo.bean.Company;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.oxm.xstream.XStreamMarshaller;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.stream.Stream;

public class Application {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext annoCtx = new AnnotationConfigApplicationContext();
        annoCtx.register(AppConfig.class);
        annoCtx.refresh();

        XStreamMarshaller xStreamMarshaller = annoCtx.getBean(XStreamMarshaller.class);

        Company company = new Company();
        company.setId(100);
        company.setCompanyName("PQR");
        company.setCeoName("MNO");
        company.setNoEmp(50);

        xStreamMarshaller.marshal(company, new StreamResult(new FileOutputStream("company.xml")));
        System.out.println("marshal success");

        Company outputCompany = (Company) xStreamMarshaller.unmarshal(new StreamSource(new FileInputStream("company.xml")));
        System.out.println(outputCompany);
    }
}
